var layer;
var user;
var staticUserRole;
$(function () {
    $("#menuId").val(sessionStorage.menuId);
    layui.use(['laydate', 'element', 'laypage', 'layer'], function () {
        $ = layui.jquery;//jquery
        laydate = layui.laydate;//日期插件
        element = layui.element();//面包导航
        laypage = layui.laypage;//分页
        layer = layui.layer;//弹出层


    });
});

//根据手机号码查找用户
function findUserByPhone() {
    var phone = $("#phone").val();
    var menuId = $("#menuId").val();
    if (phone == "") {
        alert("请输入手机号码！")
        return false;
    } else if (!(/^1[3|4|5|7|8]\d{9}$/.test(phone))) {
        alert("请输入有效的手机号码！");
        return false;
    }
    ;
    var params = {
        "type": "query",
        "menuId": menuId,
        "phone": phone
    };
    $.ajax({
        type: "post",
        url: "/user/findUserByPhone",
        data: params,
        dataType: "json",
        success: function (data) {
            if (data.code == 1 && data.code != undefined) {
                user = data.user;
                var html = "";

                var roelStr = "";
                if (user.role == "USER") {
                    roelStr = "普通用户";
                } else if (user.role == "SUPER") {
                    roelStr = "官方特权用户";
                } else if (user.role == "CHUANG") {
                    roelStr = "创客";
                } else if (user.role == "HEHUOREN") {
                    roelStr = "合伙人";
                } else if (user.role == "SERVER") {
                    roelStr = "服务商";
                }
                var birthday = '';
                if (user.birthday != "" && user.birthday != null) {
                    birthday = new Date(user.birthday).Format("yyyy-MM-dd");
                }
                html += '<tr><td>' + user.id + '</td>' +
                    '<td>' + user.phone + '</td>' +
                    '<td>' + user.nickName + '</td>' +
                    '<td>' + (user.sex == 0 ? "男" : "女") + '</td>' +
                    '<td>' + birthday + '</td>' +
                    '<td>' + roelStr + '</td>' +
                    '<td>' + (user.state == 1 ? "正常" : "封停") + '</td>' +
                    '<td><button class="layui-btn layui-btn-normal" onclick="showRoleList()">角色分配</button></td></tr>'


            } else if (data == 9999 && data != undefined) {
                window.location.href = "/login_toLogin";
            } else if (data == 1111 && data != undefined) {
                alert("无权操作");
            } else if (data.code == 2 && data.code != undefined) {
                html = '<tr><td colspan="8" style="text-align: center">查无数据</td></tr>';
            }
            $("#bus").html(html);
        }
    });
}

//显示角色列表
var index;
function showRoleList() {
    var menuId = $("#menuId").val();
    var userId = user.id;
    var params = {
        "menuId": menuId,
        "userId": userId,
        "type": "query"
    };
    $.ajax({
        type: "post",
        url: "/srole/findRoleAndUserRole",
        data: params,
        dataType: "json",
        success: function (data) {
            if (data.code == 1 && data.code != undefined) {
                var list = data.list;
                staticUserRole = data.staticUserRole;
                var html = '<div  style="padding: 1% 2%;height: 409px;overflow-y: auto"><table style="width: 98%;">';
                if (staticUserRole != null && staticUserRole != undefined) {
                    for (var b in list) {
                        if (list[b].roleId == staticUserRole.roleId) {
                            html += '<tr><td style="font-size: 20px;width: 150px;padding: 5px 5px;border-bottom: 1px solid #e2e2e2;"><label  style="cursor: pointer;font-size: 18px;color: #435e75"><input name="role" value="'+list[b].roleId+'" roleName="'+list[b].roleName+'" type="radio" checked="checked"/>' + list[b].roleName + '</label></td></tr>';
                        } else {
                            html += '<tr><td style="font-size: 20px;width: 150px;padding: 5px 5px;border-bottom: 1px solid #e2e2e2;"><label  style="cursor: pointer;font-size: 18px;color: #435e75"><input name="role" value="'+list[b].roleId+'" roleName="'+list[b].roleName+'" type="radio" />' + list[b].roleName + '</label></td></tr>';
                        }
                    }
                    html += '<tr><td style="font-size: 20px;width: 150px;padding: 5px 5px;border-bottom: 1px solid #e2e2e2;"><label  style="cursor: pointer;font-size: 18px;color: #435e75"><input name="role" value="" type="radio" />无</label></td></tr>';
                } else {
                    for (var b in list) {
                        html += '<tr><td style="font-size: 20px;width: 150px;padding: 5px 5px;border-bottom: 1px solid #e2e2e2;"><label  style="cursor: pointer;font-size: 18px;color: #435e75"><input name="role" value="'+list[b].roleId+'" roleName="'+list[b].roleName+'" type="radio"/>' + list[b].roleName + '</label></td></tr>';
                    }
                    html += '<tr><td style="font-size: 20px;width: 150px;padding: 5px 5px;border-bottom: 1px solid #e2e2e2;"><label  style="cursor: pointer;font-size: 18px;color: #435e75"><input name="role" value="" type="radio" checked />无</label></td></tr>';
                }

                html += '</tr></table></div><div style="width: 100%;text-align: center;"><button onclick="roleDistribution(this)" class="layui-btn layui-btn-normal" style="margin: 10px auto 20px;width: 200px">提交</button></div>';

                index = layer.open({
                    title: ['分配角色', 'color: #2299ee;border-bottom: 1px solid;']
                    , type: 1
                    , content: html
                    , area: ['500px', '550px']
                    , maxmin: true
                });

            } else if (data == 9999 && data != undefined) {
                window.location.href = "/login_toLogin";
            } else if (data == 1111 && data != undefined) {
                alert("无权操作");
            }
        }

    });

}

//分配角色
function roleDistribution(o) {
    $(o).attr("disabled",true);
    var menuId = $("#menuId").val();
    var obj = $('input[name="role"]:checked');
    var roleId = obj.val();
    var roleName = obj.attr("roleName");
    var userRoleId = "";
    if(staticUserRole != null && staticUserRole != undefined){
        if(roleId == staticUserRole.roleId){
            alert("修改成功");
            layer.close(index);
            return false;
        }
        userRoleId = staticUserRole.id;
    }else {
        if(roleId == ""){
            alert("修改成功");
            layer.close(index);
            return false;
        }
    }
    var params = {
        "menuId":menuId,
        "type":"change",
        "roleId":roleId,
        "roleName":roleName,
        "userRoleId":userRoleId,
        "userId":user.id,
        "phone":user.phone
    };
    $.ajax({
        type:"post",
        url:"/srole/roleDistribution",
        data:params,
        dataType:"json",
        success:function (data) {
            if(data.code == 1 && data.code != undefined){
                alert("分配成功");
                layer.close(index);
            } else if (data == 9999 && data != undefined) {
                window.location.href = "/login_toLogin";
            } else if (data == 1111 && data != undefined) {
                alert("无权操作");
                layer.close(index);
            } else if(data.code == 2 && data.code != undefined){
                alert("分配失败");
                layer.close(index);
            }else if(data.code == 3 && data.code != undefined){
                alert("分配失败");
                layer.close(index);
            }
        }
    });
}